#!/usr/bin/env bash
# Job yaml can send below ENVIRONMENT VARIABLES to me
# - install
# - repo_name
# - repo_baseurl
# - repo_priority
# - repo_gpgcheck
# - repo_gpgkey
# - repo_metalink
# - repo_metadata_expire
# END ENVIRONMENT VARIABLES

add_repo()
{
	[ -n "$repo_baseurl" ] || return

	local cfgfile=/etc/yum.repos.d/$repo_name.repo

	cat <<-EOF >> $cfgfile
	[$repo_name]
	name=$repo_name
	baseurl=$repo_baseurl
	enabled=1
	priority=${repo_priority:-100}
	gpgcheck=${repo_gpgcheck:-1}
	EOF

	[ -n "$repo_gpgkey" ] && echo "gpgkey=$repo_gpgkey" >> $cfgfile
	[ -n "$repo_metalink" ] && echo "metalink=$repo_metalink" >> $cfgfile
	[ -n "$repo_metadata_expire" ] && echo "metadata_expire=$repo_metadata_expire" >> $cfgfile

	yum -y makecache
}

[ -n "$repo_name" ] && add_repo

[ -n "$install" ] && yum install -y -q --skip-broken $install
